﻿/*
MainGame.cs 游戏主循环文件
*/

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace touhou_bullet_rain;

public class MainGame : Game {

    private GraphicsDeviceManager graphics;
    private SpriteBatch spriteBatch;

    private Color backgroundColor = new Color(65, 65, 65);

    private GameManager manager;
    private ClientProperties properties;

    public static GraphicsDevice graphicsDevice;

    public static int SCREEN_WIDTH = 1080;
    public static int SCREEN_HEIGHT = 864;

    public MainGame() {
        properties = ClientProperties.ReadProperties();
        SCREEN_WIDTH = properties.windowWidth;
        SCREEN_HEIGHT = properties.windowHeight;
        graphics = new GraphicsDeviceManager(this) {
            PreferredBackBufferWidth = SCREEN_WIDTH,
            PreferredBackBufferHeight = SCREEN_HEIGHT
        };
        graphics.ApplyChanges();
        Content.RootDirectory = "Content";
        IsMouseVisible = false;
    }

    protected override void Initialize() {
        Window.AllowUserResizing = true;
        Window.Title = "Touhou Bullet Rain";
        base.Initialize();
    }

    protected override void LoadContent() {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        graphicsDevice = GraphicsDevice;
        GameManager.Content = Content;
        GameManager.spriteBatch = spriteBatch;
        manager = new GameManager();
        manager.StartLevel("ws://" + properties.connectIP + ":" +
            properties.connectPort, properties.playerName);
        // 启动游戏行为管理器
    }

    protected override void Update(GameTime gameTime) {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();
        SCREEN_WIDTH = GraphicsDevice.PresentationParameters.BackBufferWidth;
        SCREEN_HEIGHT = GraphicsDevice.PresentationParameters.BackBufferHeight;
        manager.Update(gameTime);
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime) {
        GraphicsDevice.Clear(backgroundColor);
        spriteBatch.Begin(samplerState: SamplerState.PointClamp);
        manager.Draw();
        spriteBatch.End();
        base.Draw(gameTime);
    }
}
